import publicForm from "@/components/public/up-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    getOnworkrecordsByFid, //详情
    saveOnworkrecords, //保存修改
    getUserCompanyTreeListXm, //巡查项目部
    getfChargeuseridData, // 根据是否待岗查网格信息
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "projectId",
        "status",
        "statusNum",
        "dataFid",
        "currentRow",
        "projectName",
        "fCompanyid"
    ],
    data() {
        return {
            visible: "",
            title: "现场工作管理",
            isLock: false, // 控制按钮的disable属性
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            formData: {
                fId: "", //传fid代表修改 不传代表新增
                fProjectid: "", //当前登录人项目ID
                fProjectname: "", //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
                fProjectfullname: "",
                fCompanyid: "", //当前登录人标段ID
                fCompanyname: "", //当前登录人标段名称
                fCompanyfullname: "",
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                fOnworkname: "", //现场工作人
                fOnworkid: "", //现场工作人id
                fReportdates: this.common.getDay(0), //填报日期
                fOnworkdates: this.common.getDay(0), //现场工作日期
                fCheckprojectid: "", //巡查项目部id
                fCheckproject: "", //巡查项目部
                fIspost: "否", //是否待岗
                fQualitygridclass: "", //质量网格类别
                fChargeuserid: "", //责任人id
                fChargeusername: "", //责任人姓名
                fDuty: "", //职务
                fQualitydutydetail: "", //质量网格责任内容
                fOnworkdetail: "", //现场工作清况
                fOnworkfile: "", //现场工作照片
                recordfiles: [], //删除文件id集合
                fLongitude: "",
                fLatitude: ""
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentStatus: null,
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        projectId: {
            handler(newVal) {
                if (newVal) {
                    console.log('projectId', newVal);
                }
            },
            immediate: true,
        },
        status: {
            handler(newVal) {
                if (newVal) {
                    this.btnStatus = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        dataFid: {
            handler(newVal) {
                if (newVal) {
                    console.log(newVal);
                    this.currntData = newVal;
                    if (this.statusNum != 1) {
                        this.formData = this.$options.data.call(this).formData;
                        this.getDetail({
                            fId: newVal,
                        });
                    }
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    // this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        this.getUserCompanyTreeListXm({
            companyId: this.userInfo.companyId,
            userId: this.userInfo.userId
        })
        this.btnShow = true
        if (this.statusNum == 1) {
            this.formData = this.$options.data.call(this).formData;
            this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
            this.formData.fOnworkid = this.userInfo.userId; //现场工作人id
            this.formData.fOnworkname = this.userInfo.realName; //现场工作人名称
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fReportdates = this.common.getDay(0) //填报日期
            this.formData.fOnworkdates = this.common.getDay(0) //现场工作日期
            this.formData.fCompanyname = this.userInfo.companyName; //当前登录人标段名称
            this.formData.fCompanyid = this.userInfo.companyId; //当前登录人标段ID
            this.formData.fCompanyfullname = this.userInfo.companyName; //当前登录人标段名称
            this.formData.fProjectfullname = this.projectName; //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
            this.formData.fProjectname = this.projectName; //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
            this.formData.fProjectid = this.projectId; //当前登录人项目ID
            this.formData.fOnworkfile = Date.now() + this.userInfo.userId
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[2].disabled = false;
            this.formObj[3].disabled = false;
            // this.formObj[4].disabled = false;
            this.formObj[8].disabled = false;
            this.formObj[10].disabled = false;
            this.formObj[11].disabled = false;
            this.Ispost({
                fCompanyid: this.userInfo.companyId,
                fChargeuserid: this.userInfo.userId
            })
            this.formObj[9].uploadSelect = true;
        } else if (this.statusNum == 2) { //编辑
            //编辑
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[2].disabled = false;
            this.formObj[3].disabled = false;
            this.formObj[4].disabled = false;
            this.formObj[8].disabled = false;
            this.formObj[10].disabled = false;
            this.formObj[11].disabled = false;

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[6].uploadSelect = false;
        }
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        // fCcpeopleData计划传阅人
        selectChange(data, a, b) {
            if (b == "fIspost") { //是否待岗
                if (data == '是') {
                    this.formObj[5].disabled = false
                    this.formData.fQualitygridclass = "" //质量网格类别
                    this.formData.fChargeuserid = "" //责任人id
                    this.formData.fChargeusername = "" //责任人姓名
                    this.formData.fDuty = "" //职务
                    this.formData.fQualitydutydetail = ""
                    this.Ispost({
                        fCompanyid: this.formData.fCompanyid,
                        companyid: this.formData.fCheckprojectid,
                        stationType: 1,
                        fChargeuserid: this.userInfo.userId
                    })

                } else {
                    this.formObj[5].disabled = false
                    this.formData.fQualitygridclass = "" //质量网格类别
                    this.formData.fChargeuserid = "" //责任人id
                    this.formData.fChargeusername = "" //责任人姓名
                    this.formData.fDuty = "" //职务
                    this.formData.fQualitydutydetail = ""
                    this.formObj[5].disabled = true
                    this.Ispost({
                        fChargeuserid: this.userInfo.userId,
                        fCompanyid: this.formData.fCheckprojectid
                    })
                }
            }
            if (b == 'fChargeusername') {
                this.formData.fQualitygridclass = data.fQualitygridclass //质量网格类别
                this.formData.fChargeuserid = data.value //责任人id
                this.formData.fChargeusername = data.label //责任人姓名
                this.formData.fDuty = data.fDuty //职务
                this.formData.fQualitydutydetail = data.fQualitydutydetail
            }
            if (b == 'fCheckproject') {
                this.formData.fCheckproject = data.label
                this.formData.fCheckprojectid = data.value
                this.Ispost({
                    fCompanyid: data.value,
                    fChargeuserid: this.formData.fOnworkid
                })
                this.formObj[4].disabled = false;
                this.formData.fIspost = '否'
                this.formData.fQualitygridclass = "" //质量网格类别
                this.formData.fChargeuserid = "" //责任人id
                this.formData.fChargeusername = "" //责任人姓名
                this.formData.fDuty = "" //职务
                this.formData.fQualitydutydetail = ""

            }
        },
        Ispost(data) { //是否待岗
            getfChargeuseridData(data).then(res => {
                console.log('//是否待岗', this.formData.fIspost);
                if (res.errcode == 0) {
                    if (this.formData.fIspost == '否') {
                        var obj = res.data[0]
                        this.formData.fQualitygridclass = obj.fQualitygridclass //质量网格类别
                        this.formData.fChargeuserid = obj.fChargeuserid //责任人id
                        this.formData.fChargeusername = obj.fChargeusername //责任人姓名
                        this.formData.fDuty = obj.fDuty //职务
                        this.formData.fQualitydutydetail = obj.fQualitydutydetail
                    } // this.formObj[5].options = obj
                    else {
                        this.formObj[5].options = []
                        res.data.map(item => {
                            this.formObj[5].options.push({
                                value: item.fChargeuserid,
                                label: item.fChargeusername,
                                fDuty: item.fDuty,
                                fQualitydutydetail: item.fQualitydutydetail,
                                fQualitygridclass: item.fQualitygridclass
                            })
                        })
                    }
                }
            })
        },
        // 巡查项目部
        getUserCompanyTreeListXm(data) {
            getUserCompanyTreeListXm(data).then(res => {
                if (res.errcode == 0) {
                    this.formObj[2].options = []
                    res.data.userCompanyTreeList.map(item => {
                        // console.log(item);
                        if(item.fNature=='施工单位'){
                            this.formObj[2].options.push({
                                label: item.fShortname,
                                value: item.fCompanyid
                            })
                        }
                        
                    })
                }
            })
        },
        edit(status) {
            if (status == 1) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 2) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 4) {
                this.monthExamineData = this.$options.data.call(this).monthExamineData;
            }
        },
        // 详情
        getDetail(data1) {
            getOnworkrecordsByFid(data1).then((res) => {
                var data = res.data[0];
                if (data1.fId != '') {
                    this.formData = data
                    this.formDatafileList = res.data[0].fOnworkfilelist
                    this.formData.recordfiles = []
                    // this.formDatafileList = res.data
                } else {
                    this.formData.fOnworkfile = data.fOnworkfile //资料附件ID
                    this.formData.fQualitygridclass = data.fQualitygridclass //质量网格类别
                    this.formData.fChargeuserid = data.fChargeuserid //责任人id
                    this.formData.fChargeusername = data.fChargeusername //责任人姓名
                    this.formData.fDuty = data.fDuty //职务
                    this.formData.recordfiles = []
                }
            });
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.formDatafileList.length; index++) {
                if (file.uid == this.formDatafileList[index].uid) {
                    if (this.formDatafileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.formDatafileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.formDatafileList[index].atta.fId,
                        });
                    }
                    this.formDatafileList.splice(index, 1);
                }
            }
        },
        yj_saveForm() {

            if (this.statusNum == 1) { //新增
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            } else if (this.statusNum == 2) { //编辑
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            }
        },

        // 保存
        saveForm(data) {
            this.isLock = true
            saveOnworkrecords(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.isLock = false
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");

                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },
    },
};